﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Orders.Views;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Autorent.Desktop.Modules.Orders.Reports;
using Autorent.Desktop.Modules.Orders.Reports.ReportsDataSets;

namespace Autorent.Desktop.Modules.Orders.ViewModels
{
    public class PayReportViewModel : ViewModelBase
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        public PayReportViewModel(PayReportView view, AutoOrder autoOrder, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.View = view;
            View.Model = this;

            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            PayReportDataSet ds = new PayReportDataSet();
            ds.PayReportTable.AddPayReportTableRow(
                (int)autoOrder.AutoOrder_ID,
                autoOrder.Client.ClientLastName,
                autoOrder.Client.ClientFirstName,
                autoOrder.Client.ClientPatroname,
                autoOrder.AutoClass.AutoClassName,
                autoOrder.Auto.AutoModel,
                autoOrder.Auto.AutoNumber,
                autoOrder.Office.OfficeName,
                (DateTime)autoOrder.AutoOrderWithdrawalDT,
                autoOrder.Office1.OfficeName,
                (DateTime)autoOrder.AutoOrderReturningDT,
                (decimal)autoOrder.Payment.PaymentValue);

            PayReport report = new PayReport();
            report.SetDataSource(ds);

            View.reportViewer.ViewerCore.ReportSource = report;
        }

        public PayReportView View { get; set; }

        public ICommand CloseReport
        {
            get
            {
                return new RelayCommand(param => closeReport());
            }
        }

        private void closeReport()
        {
            _regionManager.Regions["ApplicationContentRegion"].Remove(this.View);
            _eventAggregator.GetEvent<ViewClosedEvent>().Publish(this.View.GetType());
        }
    }
}
